/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZFolder.View;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.Options;
/*     */ import com.zimbra.client.ZMountpoint;
/*     */ import com.zimbra.client.ZSearchFolder;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.SearchFolder;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import java.nio.ByteBuffer;
/*     */ import java.nio.charset.Charset;
/*     */ import java.util.Set;
/*     */ 
/*     */ public class ImapPath
/*     */   implements Comparable<ImapPath>
/*     */ {
/*     */   static Charset FOLDER_ENCODING_CHARSET;
/*     */   static final String NAMESPACE_PREFIX = "/home/";
/*     */   private ImapCredentials mCredentials;
/*     */   private String mOwner;
/*     */   private String mPath;
/*     */   private ItemId mItemId;
/*     */   
/*     */   static enum Scope
/*     */   {
/*  50 */     UNPARSED,  NAME,  CONTENT,  REFERENCE;
/*     */     
/*     */     private Scope() {}
/*     */   }
/*     */   
/*  55 */   static { try { FOLDER_ENCODING_CHARSET = Charset.forName("imap-utf-7");
/*     */     } catch (Exception e) {
/*  57 */       ZimbraLog.imap.error("could not load imap-utf-7 charset (perhaps zimbra-charset.jar is not in the jetty endorsed directory)", e);
/*  58 */       FOLDER_ENCODING_CHARSET = Charset.forName("utf-8");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  68 */   private Scope mScope = Scope.CONTENT;
/*     */   
/*     */ 
/*     */   private transient Object mMailbox;
/*     */   
/*     */ 
/*     */   private transient Object mFolder;
/*     */   
/*     */ 
/*     */   private transient ImapPath mReferent;
/*     */   
/*     */ 
/*     */   ImapPath(String imapPath, ImapCredentials creds)
/*     */   {
/*  82 */     this(imapPath, creds, Scope.CONTENT);
/*     */   }
/*     */   
/*     */   ImapPath(String imapPath, ImapCredentials creds, Scope scope) {
/*  86 */     this.mCredentials = creds;
/*  87 */     this.mPath = imapPath;
/*  88 */     this.mScope = scope;
/*     */     
/*  90 */     if (imapPath.toLowerCase().startsWith("/home/")) {
/*  91 */       imapPath = imapPath.substring("/home/".length());
/*  92 */       if ((!imapPath.equals("")) && (!imapPath.startsWith("/"))) {
/*  93 */         int slash = imapPath.indexOf('/');
/*  94 */         this.mOwner = (slash == -1 ? imapPath : imapPath.substring(0, slash)).toLowerCase();
/*  95 */         this.mPath = (slash == -1 ? "" : imapPath.substring(slash));
/*     */       }
/*     */     }
/*     */     
/*  99 */     while (this.mPath.startsWith("/")) {
/* 100 */       this.mPath = this.mPath.substring(1);
/*     */     }
/* 102 */     while (this.mPath.endsWith("/")) {
/* 103 */       this.mPath = this.mPath.substring(0, this.mPath.length() - 1);
/*     */     }
/*     */     
/*     */ 
/* 107 */     String lcname = this.mPath.toLowerCase();
/* 108 */     if ((creds != null) && (creds.isHackEnabled(ImapCredentials.EnabledHack.WM5)) && 
/* 109 */       (lcname.startsWith("sent items")) && ((lcname.length() == 10) || (lcname.charAt(10) == '/'))) {
/* 110 */       this.mPath = ("Sent" + this.mPath.substring(10));
/*     */     }
/*     */   }
/*     */   
/*     */   ImapPath(String owner, String zimbraPath, ImapCredentials creds)
/*     */   {
/* 116 */     this.mCredentials = creds;
/* 117 */     this.mOwner = (owner == null ? null : owner.toLowerCase());
/* 118 */     this.mPath = (zimbraPath.startsWith("/") ? zimbraPath.substring(1) : zimbraPath);
/*     */   }
/*     */   
/*     */   ImapPath(ImapPath other) {
/* 122 */     this.mCredentials = other.mCredentials;
/* 123 */     this.mOwner = other.mOwner;
/* 124 */     this.mPath = other.mPath;
/* 125 */     this.mMailbox = other.mMailbox;
/* 126 */     this.mFolder = other.mFolder;
/* 127 */     this.mItemId = other.mItemId;
/*     */   }
/*     */   
/*     */   ImapPath(String owner, Folder folder, ImapCredentials creds) {
/* 131 */     this(owner, folder.getPath(), creds);
/* 132 */     this.mMailbox = folder.getMailbox();
/* 133 */     this.mFolder = folder;
/* 134 */     this.mItemId = new ItemId(folder);
/*     */   }
/*     */   
/*     */   ImapPath(String owner, Folder folder, ImapPath mountpoint) throws ServiceException {
/* 138 */     this(mountpoint);
/* 139 */     (this.mReferent = new ImapPath(owner, folder, this.mCredentials)).mScope = Scope.REFERENCE;
/* 140 */     int start = mountpoint.getReferent().mPath.length() + 1;
/* 141 */     this.mPath = (mountpoint.mPath + "/" + this.mReferent.mPath.substring(start == 1 ? 0 : start));
/*     */   }
/*     */   
/*     */   ImapPath(String owner, ZFolder zfolder, ImapCredentials creds) throws ServiceException {
/* 145 */     this(owner, zfolder.getPath(), creds);
/* 146 */     this.mMailbox = zfolder.getMailbox();
/* 147 */     this.mFolder = zfolder;
/* 148 */     this.mItemId = new ItemId(zfolder.getId(), creds == null ? null : creds.getAccountId());
/*     */   }
/*     */   
/*     */   ImapPath(String owner, ZFolder zfolder, ImapPath mountpoint) throws ServiceException {
/* 152 */     this(mountpoint);
/* 153 */     (this.mReferent = new ImapPath(owner, zfolder, this.mCredentials)).mScope = Scope.REFERENCE;
/* 154 */     int start = mountpoint.getReferent().mPath.length() + 1;
/* 155 */     this.mPath = (mountpoint.mPath + "/" + this.mReferent.mPath.substring(start == 1 ? 0 : start));
/*     */   }
/*     */   
/*     */   public boolean isEquivalent(ImapPath other)
/*     */   {
/* 160 */     if (!this.mPath.equalsIgnoreCase(other.mPath))
/* 161 */       return false;
/* 162 */     if ((this.mOwner == other.mOwner) || ((this.mOwner != null) && (this.mOwner.equalsIgnoreCase(other.mOwner)))) {
/* 163 */       return true;
/*     */     }
/*     */     try {
/* 166 */       Account acct = getOwnerAccount();Account otheracct = other.getOwnerAccount();
/* 167 */       return (acct == null) || (otheracct == null) ? false : acct.getId().equalsIgnoreCase(otheracct.getId());
/*     */     } catch (ServiceException e) {}
/* 169 */     return false;
/*     */   }
/*     */   
/*     */   public boolean equals(Object obj)
/*     */   {
/* 174 */     if (!(obj instanceof ImapPath)) {
/* 175 */       return super.equals(obj);
/*     */     }
/* 177 */     return asImapPath().equalsIgnoreCase(((ImapPath)obj).asImapPath());
/*     */   }
/*     */   
/*     */   public int compareTo(ImapPath o) {
/* 181 */     return asImapPath().compareToIgnoreCase(o.asImapPath());
/*     */   }
/*     */   
/*     */   public int hashCode() {
/* 185 */     return (this.mOwner == null ? 0 : this.mOwner.toUpperCase().hashCode()) ^ this.mPath.toUpperCase().hashCode() ^ (this.mCredentials == null ? 0 : this.mCredentials.hashCode());
/*     */   }
/*     */   
/*     */   ImapPath canonicalize() throws ServiceException {
/* 189 */     getFolder();
/*     */     String path;
/*     */     String path;
/* 192 */     if ((this.mFolder instanceof Folder)) {
/* 193 */       path = ((Folder)this.mFolder).getPath();
/*     */     } else {
/* 195 */       path = ((ZFolder)this.mFolder).getPath();
/*     */     }
/*     */     
/* 198 */     while (path.startsWith("/")) {
/* 199 */       path = path.substring(1);
/*     */     }
/* 201 */     while (path.endsWith("/")) {
/* 202 */       path = path.substring(0, path.length() - 1);
/*     */     }
/*     */     
/* 205 */     int excess = this.mPath.length() - path.length();
/* 206 */     if ((this.mReferent == this) || (excess == 0)) {
/* 207 */       this.mPath = path;
/*     */     } else {
/* 209 */       this.mPath = (path + "/" + this.mReferent.canonicalize().mPath.substring(this.mReferent.mPath.length() - excess + 1));
/*     */     }
/* 211 */     return this;
/*     */   }
/*     */   
/*     */   String getOwner()
/*     */   {
/* 216 */     return this.mOwner;
/*     */   }
/*     */   
/*     */   ImapCredentials getCredentials() {
/* 220 */     return this.mCredentials;
/*     */   }
/*     */   
/*     */   boolean belongsTo(Mailbox mbox) throws ServiceException {
/* 224 */     return belongsTo(mbox.getAccountId());
/*     */   }
/*     */   
/*     */   boolean belongsTo(ImapCredentials creds) throws ServiceException {
/* 228 */     return belongsTo(creds.getAccountId());
/*     */   }
/*     */   
/*     */   boolean belongsTo(String accountId) throws ServiceException {
/* 232 */     String ownerId = getOwnerAccountId();
/* 233 */     return (ownerId != null) && (ownerId.equalsIgnoreCase(accountId));
/*     */   }
/*     */   
/*     */   String getOwnerAccountId() throws ServiceException {
/* 237 */     if (useReferent()) {
/* 238 */       return getReferent().getOwnerAccountId();
/*     */     }
/*     */     
/* 241 */     if ((this.mMailbox instanceof Mailbox))
/* 242 */       return ((Mailbox)this.mMailbox).getAccountId();
/* 243 */     if ((this.mOwner == null) && (this.mCredentials != null))
/* 244 */       return this.mCredentials.getAccountId();
/* 245 */     if (this.mOwner == null) {
/* 246 */       return null;
/*     */     }
/* 248 */     Account acct = getOwnerAccount();
/* 249 */     return acct == null ? null : acct.getId();
/*     */   }
/*     */   
/*     */   Account getOwnerAccount() throws ServiceException {
/* 253 */     if (useReferent())
/* 254 */       return getReferent().getOwnerAccount();
/* 255 */     if ((this.mMailbox instanceof Mailbox))
/* 256 */       return ((Mailbox)this.mMailbox).getAccount();
/* 257 */     if (this.mOwner != null)
/* 258 */       return Provisioning.getInstance().get(Key.AccountBy.name, this.mOwner);
/* 259 */     if (this.mCredentials != null) {
/* 260 */       return Provisioning.getInstance().get(Key.AccountBy.id, this.mCredentials.getAccountId());
/*     */     }
/* 262 */     return null;
/*     */   }
/*     */   
/*     */   boolean onLocalServer() throws ServiceException
/*     */   {
/* 267 */     Account acct = getOwnerAccount();
/* 268 */     return (acct != null) && (Provisioning.onLocalServer(acct));
/*     */   }
/*     */   
/*     */   Object getOwnerMailbox() throws ServiceException {
/* 272 */     if (useReferent()) {
/* 273 */       return this.mReferent.getOwnerMailbox();
/*     */     }
/* 275 */     if (this.mMailbox == null) {
/* 276 */       Account target = getOwnerAccount();
/* 277 */       if (target == null) {
/* 278 */         this.mMailbox = null;
/* 279 */       } else if (Provisioning.onLocalServer(target)) {
/* 280 */         this.mMailbox = MailboxManager.getInstance().getMailboxByAccount(target);
/* 281 */       } else if (this.mCredentials == null) {
/* 282 */         this.mMailbox = null;
/*     */       } else {
/* 284 */         this.mMailbox = getOwnerZMailbox();
/*     */       }
/*     */     }
/* 287 */     return this.mMailbox;
/*     */   }
/*     */   
/*     */   ZMailbox getOwnerZMailbox() throws ServiceException {
/* 291 */     if (useReferent()) {
/* 292 */       return getReferent().getOwnerZMailbox();
/*     */     }
/*     */     
/* 295 */     if ((this.mMailbox instanceof ZMailbox))
/* 296 */       return (ZMailbox)this.mMailbox;
/* 297 */     if (this.mCredentials == null) {
/* 298 */       return null;
/*     */     }
/*     */     
/* 301 */     Account target = getOwnerAccount();
/* 302 */     if (target == null) {
/* 303 */       throw AccountServiceException.NO_SUCH_ACCOUNT(getOwner());
/*     */     }
/* 305 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.id, this.mCredentials.getAccountId());
/* 306 */     if (acct == null) {
/* 307 */       throw AccountServiceException.NO_SUCH_ACCOUNT(this.mCredentials.getUsername());
/*     */     }
/*     */     try
/*     */     {
/* 311 */       ZMailbox.Options options = new ZMailbox.Options(AuthProvider.getAuthToken(acct).getEncoded(), AccountUtil.getSoapUri(target));
/* 312 */       options.setTargetAccount(target.getName());
/* 313 */       options.setNoSession(true);
/* 314 */       return ZMailbox.getMailbox(options);
/*     */     } catch (AuthTokenException ate) {
/* 316 */       throw ServiceException.FAILURE("error generating auth token", ate);
/*     */     }
/*     */   }
/*     */   
/*     */   private OperationContext getContext() throws ServiceException {
/* 321 */     return this.mCredentials == null ? null : this.mCredentials.getContext();
/*     */   }
/*     */   
/*     */   Object getFolder() throws ServiceException {
/* 325 */     if (useReferent()) {
/* 326 */       return getReferent().getFolder();
/*     */     }
/*     */     
/* 329 */     if ((this.mCredentials != null) && (this.mCredentials.isFolderHidden(this))) {
/* 330 */       throw MailServiceException.NO_SUCH_FOLDER(asImapPath());
/*     */     }
/*     */     
/* 333 */     if (this.mFolder == null) {
/* 334 */       Object mboxobj = getOwnerMailbox();
/* 335 */       if ((mboxobj instanceof Mailbox)) {
/* 336 */         Folder folder = ((Mailbox)mboxobj).getFolderByPath(getContext(), asZimbraPath());
/* 337 */         this.mFolder = folder;
/* 338 */         this.mItemId = new ItemId(folder);
/* 339 */       } else if ((mboxobj instanceof ZMailbox)) {
/* 340 */         ZFolder zfolder = ((ZMailbox)mboxobj).getFolderByPath(asZimbraPath());
/* 341 */         this.mFolder = zfolder;
/* 342 */         if (zfolder == null) {
/* 343 */           throw MailServiceException.NO_SUCH_FOLDER(asImapPath());
/*     */         }
/* 345 */         this.mItemId = new ItemId(zfolder.getId(), this.mCredentials == null ? null : this.mCredentials.getAccountId());
/*     */       } else {
/* 347 */         throw AccountServiceException.NO_SUCH_ACCOUNT(getOwner());
/*     */       }
/*     */     }
/* 350 */     return this.mFolder;
/*     */   }
/*     */   
/*     */   boolean useReferent() throws ServiceException {
/* 354 */     if (getReferent() == this)
/* 355 */       return false;
/* 356 */     if (this.mScope == Scope.CONTENT) {
/* 357 */       return true;
/*     */     }
/*     */     
/*     */ 
/* 361 */     assert (this.mScope == Scope.NAME);
/* 362 */     assert (this.mFolder != null);
/* 363 */     assert (this.mReferent != null);
/*     */     
/*     */     ItemId iidBase;
/* 366 */     if ((this.mFolder instanceof Mountpoint)) {
/* 367 */       iidBase = ((Mountpoint)this.mFolder).getTarget(); } else { ItemId iidBase;
/* 368 */       if ((this.mFolder instanceof ZMountpoint)) {
/* 369 */         iidBase = new ItemId(((ZMountpoint)this.mFolder).getCanonicalRemoteId(), (String)null);
/*     */       } else
/* 371 */         return false; }
/*     */     ItemId iidBase;
/* 373 */     return !iidBase.equals(this.mReferent.mItemId);
/*     */   }
/*     */   
/*     */   ImapPath getReferent() throws ServiceException {
/* 377 */     if (this.mReferent != null) {
/* 378 */       return this.mReferent;
/*     */     }
/*     */     
/*     */ 
/* 382 */     this.mReferent = this;
/*     */     
/*     */ 
/* 385 */     if ((this.mScope == Scope.REFERENCE) || (this.mScope == Scope.UNPARSED) || (!belongsTo(this.mCredentials))) {
/* 386 */       return this.mReferent;
/*     */     }
/*     */     
/*     */ 
/* 390 */     String subpathRemote = null;
/*     */     
/* 392 */     Object mboxobj = getOwnerMailbox();
/* 393 */     ItemId iidRemote; if ((mboxobj instanceof Mailbox))
/*     */       try {
/* 395 */         if (this.mFolder == null) {
/* 396 */           Pair<Folder, String> resolved = ((Mailbox)mboxobj).getFolderByPathLongestMatch(getContext(), 1, asZimbraPath());
/* 397 */           subpathRemote = (String)resolved.getSecond();
/*     */           
/* 399 */           boolean isMountpoint = resolved.getFirst() instanceof Mountpoint;
/* 400 */           if ((isMountpoint) || (resolved.getSecond() == null)) {
/* 401 */             this.mFolder = resolved.getFirst();
/* 402 */             this.mItemId = new ItemId((MailItem)resolved.getFirst());
/*     */           }
/* 404 */           if (!isMountpoint) {
/* 405 */             return this.mReferent;
/*     */           }
/* 407 */         } else if (!(this.mFolder instanceof Mountpoint)) {
/* 408 */           return this.mReferent;
/*     */         }
/*     */         
/*     */ 
/* 412 */         iidRemote = ((Mountpoint)this.mFolder).getTarget();
/*     */       } catch (ServiceException e) {
/* 414 */         return this.mReferent;
/*     */       }
/* 416 */     if ((mboxobj instanceof ZMailbox)) {
/* 417 */       String accountId = this.mCredentials == null ? null : this.mCredentials.getAccountId();
/* 418 */       if (this.mFolder == null) {
/* 419 */         ZMailbox zmbx = (ZMailbox)mboxobj;
/* 420 */         String path = asZimbraPath();
/*     */         try {
/* 422 */           for (int index = path.length(); index != -1; index = path.lastIndexOf('/', index - 1)) {
/* 423 */             ZFolder zfolder = zmbx.getFolderByPath(path.substring(0, index));
/* 424 */             if (zfolder != null) {
/* 425 */               subpathRemote = path.substring(Math.min(path.length(), index + 1));
/*     */               
/* 427 */               if ((!(zfolder instanceof ZMountpoint)) && (!subpathRemote.isEmpty())) break;
/* 428 */               this.mFolder = zfolder;
/* 429 */               this.mItemId = new ItemId(zfolder.getId(), accountId); break;
/*     */             }
/*     */           }
/*     */         }
/*     */         catch (ServiceException e) {}
/*     */       }
/*     */       
/*     */ 
/* 437 */       if (!(this.mFolder instanceof ZMountpoint)) {
/* 438 */         return this.mReferent;
/*     */       }
/*     */       
/*     */ 
/* 442 */       iidRemote = new ItemId(((ZMountpoint)this.mFolder).getCanonicalRemoteId(), accountId);
/*     */     } else {
/* 444 */       return this.mReferent;
/*     */     }
/*     */     
/*     */     ItemId iidRemote;
/* 448 */     if (belongsTo(iidRemote.getAccountId())) {
/* 449 */       return this.mReferent;
/*     */     }
/*     */     
/* 452 */     Account target = Provisioning.getInstance().get(Key.AccountBy.id, iidRemote.getAccountId());
/* 453 */     if (target == null) {
/* 454 */       return this.mReferent;
/*     */     }
/*     */     
/* 457 */     String owner = (this.mCredentials != null) && (this.mCredentials.getAccountId().equalsIgnoreCase(target.getId())) ? null : target.getName();
/* 458 */     if (Provisioning.onLocalServer(target)) {
/*     */       try {
/* 460 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(target);
/* 461 */         Folder folder = mbox.getFolderById(getContext(), iidRemote.getId());
/* 462 */         if (subpathRemote == null) {
/* 463 */           this.mReferent = new ImapPath(owner, folder, this.mCredentials);
/*     */         } else {
/* 465 */           (this.mReferent = new ImapPath(owner, folder.getPath() + (folder.getPath().equals("/") ? "" : "/") + subpathRemote, this.mCredentials)).mMailbox = mbox;
/*     */         }
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */     } else {
/* 470 */       Account acct = this.mCredentials == null ? null : Provisioning.getInstance().get(Key.AccountBy.id, this.mCredentials.getAccountId());
/* 471 */       if (acct == null)
/* 472 */         return this.mReferent;
/*     */       try {
/* 474 */         ZMailbox.Options options = new ZMailbox.Options(AuthProvider.getAuthToken(acct).getEncoded(), AccountUtil.getSoapUri(target));
/* 475 */         options.setTargetAccount(target.getName());
/* 476 */         options.setNoSession(true);
/* 477 */         ZMailbox zmbx = ZMailbox.getMailbox(options);
/* 478 */         ZFolder zfolder = zmbx.getFolderById(iidRemote.toString(this.mCredentials.getAccountId()));
/* 479 */         if (zfolder == null) {
/* 480 */           return this.mReferent;
/*     */         }
/* 482 */         if (subpathRemote == null) {
/* 483 */           this.mReferent = new ImapPath(owner, zfolder, this.mCredentials);
/*     */         } else {
/* 485 */           (this.mReferent = new ImapPath(owner, zfolder.getPath() + (zfolder.getPath().equals("/") ? "" : "/") + subpathRemote, this.mCredentials)).mMailbox = zmbx;
/*     */         }
/*     */       } catch (AuthTokenException ate) {
/* 488 */         throw ServiceException.FAILURE("error generating auth token", ate);
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */     }
/*     */     
/* 493 */     if (this.mReferent != this) {
/* 494 */       this.mReferent.mScope = Scope.REFERENCE;
/*     */     }
/* 496 */     return this.mReferent;
/*     */   }
/*     */   
/*     */   short getFolderRights() throws ServiceException {
/* 500 */     if ((getFolder() instanceof Folder)) {
/* 501 */       Folder folder = (Folder)getFolder();
/* 502 */       return folder.getMailbox().getEffectivePermissions(getContext(), folder.getId(), folder.getType());
/*     */     }
/* 504 */     ZFolder zfolder = (ZFolder)getFolder();
/* 505 */     String rights = zfolder.getEffectivePerms();
/* 506 */     return rights == null ? -1 : ACL.stringToRights(rights);
/*     */   }
/*     */   
/*     */ 
/*     */   boolean isCreatable()
/*     */   {
/* 512 */     String path = this.mPath.toLowerCase();
/* 513 */     return (!path.matches("\\s*notebook\\s*(/.*)?")) && (!path.matches("\\s*contacts\\s*(/.*)?")) && (!path.matches("\\s*calendar\\s*(/.*)?"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean isWritable()
/*     */     throws ServiceException
/*     */   {
/* 524 */     return (isWritable((short)8)) || (isWritable((short)4)) || (isWritable((short)2));
/*     */   }
/*     */   
/*     */   boolean isWritable(short rights)
/*     */     throws ServiceException
/*     */   {
/* 530 */     if (!isSelectable()) {
/* 531 */       return false;
/*     */     }
/*     */     
/* 534 */     if ((this.mFolder instanceof Folder)) {
/* 535 */       Folder folder = (Folder)this.mFolder;
/* 536 */       if (((folder instanceof SearchFolder)) || (folder.getDefaultView() == MailItem.Type.CONTACT)) {
/* 537 */         return false;
/*     */       }
/*     */     } else {
/* 540 */       ZFolder zfolder = (ZFolder)this.mFolder;
/* 541 */       if (((zfolder instanceof ZSearchFolder)) || (zfolder.getDefaultView() == ZFolder.View.contact)) {
/* 542 */         return false;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 547 */     if (rights == 0) {
/* 548 */       return true;
/*     */     }
/* 550 */     return (getFolderRights() & rights) == rights;
/*     */   }
/*     */   
/*     */   boolean isSelectable() throws ServiceException {
/* 554 */     if (!isVisible()) {
/* 555 */       return false;
/*     */     }
/* 557 */     if ((this.mFolder instanceof Folder)) {
/* 558 */       Folder folder = (Folder)this.mFolder;
/* 559 */       if (folder.getId() == 1)
/* 560 */         return false;
/* 561 */       if (folder.isTagged(Flag.FlagInfo.DELETED)) {
/* 562 */         return false;
/*     */       }
/*     */     } else {
/* 565 */       ZFolder zfolder = (ZFolder)this.mFolder;
/* 566 */       if (new ItemId(zfolder.getId(), (String)null).getId() == 1)
/* 567 */         return false;
/* 568 */       if (zfolder.isIMAPDeleted()) {
/* 569 */         return false;
/*     */       }
/*     */     }
/* 572 */     return this.mReferent == this ? true : this.mReferent.isSelectable();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private boolean isVisible(MailItem.Type type)
/*     */   {
/* 579 */     switch (type) {
/*     */     case APPOINTMENT: 
/*     */     case TASK: 
/*     */     case WIKI: 
/*     */     case DOCUMENT: 
/* 584 */       return false;
/*     */     }
/* 586 */     return true;
/*     */   }
/*     */   
/*     */   boolean isVisible()
/*     */     throws ServiceException
/*     */   {
/* 592 */     if (((this.mFolder instanceof Folder)) && (!isVisible(((Folder)this.mFolder).getDefaultView()))) {
/* 593 */       return false;
/*     */     }
/*     */     
/* 596 */     if ((this.mCredentials != null) && 
/* 597 */       (this.mCredentials.isFolderHidden(this))) {
/* 598 */       return false;
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 603 */       getFolder();
/*     */     } catch (ServiceException e) {
/* 605 */       if ("service.PERM_DENIED".equals(e.getCode())) {
/* 606 */         return false;
/*     */       }
/* 608 */       throw e;
/*     */     }
/*     */     
/* 611 */     if ((this.mFolder instanceof Folder)) {
/* 612 */       Folder folder = (Folder)this.mFolder;
/* 613 */       if (folder.isHidden()) {
/* 614 */         return false;
/*     */       }
/*     */     }
/* 617 */     boolean okPath = isValidImapPath();
/* 618 */     if (!okPath) {
/* 619 */       return false;
/*     */     }
/* 621 */     return this.mReferent == this ? true : this.mReferent.isVisible();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   boolean isValidImapPath()
/*     */     throws ServiceException
/*     */   {
/* 629 */     if ((this.mCredentials != null) && 
/* 630 */       (this.mCredentials.isHackEnabled(ImapCredentials.EnabledHack.WM5))) {
/* 631 */       String lcname = this.mPath.toLowerCase();
/* 632 */       if ((lcname.startsWith("sent items")) && ((lcname.length() == 10) || (lcname.charAt(10) == '/'))) {
/* 633 */         return false;
/*     */       }
/*     */     }
/*     */     try
/*     */     {
/* 638 */       if ((this.mOwner != null) && (belongsTo(this.mCredentials)))
/* 639 */         return false;
/* 640 */       getFolder();
/* 641 */       if ((this.mFolder instanceof Folder)) {
/* 642 */         Folder folder = (Folder)this.mFolder;
/*     */         
/* 644 */         if ((folder.getId() == 1) && (this.mScope != Scope.REFERENCE)) {
/* 645 */           return false;
/*     */         }
/*     */         
/* 648 */         if ((folder.getId() == 4) && (!getOwnerAccount().isFeatureAntispamEnabled())) {
/* 649 */           return false;
/*     */         }
/* 651 */         if (!isVisible(folder.getDefaultView())) {
/* 652 */           return false;
/*     */         }
/*     */         
/* 655 */         if ((this.mReferent != this) && (folder.inTrash()) && (!((Mountpoint)folder).getTarget().equals(this.mReferent.asItemId())))
/*     */         {
/* 657 */           return false;
/*     */         }
/*     */         
/* 660 */         if (((folder instanceof Mountpoint)) && (this.mReferent == this) && (this.mScope != Scope.UNPARSED)) {
/* 661 */           return false;
/*     */         }
/*     */         
/* 664 */         if ((folder instanceof SearchFolder)) {
/* 665 */           return (((SearchFolder)folder).isImapVisible()) && (ImapFolder.getTypeConstraint((SearchFolder)folder).size() > 0);
/*     */         }
/*     */       }
/*     */       else {
/* 669 */         ZFolder zfolder = (ZFolder)this.mFolder;
/* 670 */         int folderId = asItemId().getId();
/*     */         
/* 672 */         if ((folderId == 1) && (this.mScope != Scope.REFERENCE)) {
/* 673 */           return false;
/*     */         }
/*     */         
/* 676 */         if ((folderId == 4) && (!getOwnerAccount().isFeatureAntispamEnabled())) {
/* 677 */           return false;
/*     */         }
/*     */         
/* 680 */         ZFolder.View view = zfolder.getDefaultView();
/* 681 */         if ((view == ZFolder.View.appointment) || (view == ZFolder.View.task) || (view == ZFolder.View.wiki) || (view == ZFolder.View.document))
/*     */         {
/* 683 */           return false;
/*     */         }
/*     */         
/* 686 */         if (((zfolder instanceof ZMountpoint)) && (this.mReferent == this) && (this.mScope != Scope.UNPARSED)) {
/* 687 */           return false;
/*     */         }
/*     */         
/* 690 */         if ((zfolder instanceof ZSearchFolder)) {
/* 691 */           return false;
/*     */         }
/*     */         
/*     */       }
/*     */       
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException ignore) {}catch (AccountServiceException ase)
/*     */     {
/* 699 */       if (!"account.NO_SUCH_ACCOUNT".equals(ase.getCode())) {
/* 700 */         throw ase;
/*     */       }
/*     */     } catch (ServiceException se) {
/* 703 */       if ("service.PERM_DENIED".equals(se.getCode()))
/*     */       {
/* 705 */         return true;
/*     */       }
/* 707 */       throw se;
/*     */     }
/* 709 */     return this.mReferent == this ? true : this.mReferent.isValidImapPath();
/*     */   }
/*     */   
/*     */   String asZimbraPath()
/*     */   {
/* 714 */     return this.mPath;
/*     */   }
/*     */   
/*     */   String asResolvedPath() throws ServiceException {
/* 718 */     return getReferent().mPath;
/*     */   }
/*     */   
/*     */   ItemId asItemId() throws ServiceException {
/* 722 */     if (useReferent()) {
/* 723 */       return getReferent().mItemId;
/*     */     }
/*     */     
/* 726 */     if (this.mItemId == null) {
/* 727 */       getFolder();
/*     */     }
/* 729 */     return this.mItemId;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 734 */     return asImapPath();
/*     */   }
/*     */   
/*     */   String asImapPath() {
/* 738 */     String path = this.mPath;String lcpath = path.toLowerCase();
/*     */     
/* 740 */     if ((lcpath.startsWith("inbox")) && ((lcpath.length() == 5) || (lcpath.charAt(5) == '/'))) {
/* 741 */       path = "INBOX" + path.substring(5);
/* 742 */     } else if ((this.mCredentials != null) && (this.mCredentials.isHackEnabled(ImapCredentials.EnabledHack.WM5)) && 
/* 743 */       (lcpath.startsWith("sent")) && ((lcpath.length() == 4) || (lcpath.charAt(4) == '/'))) {
/* 744 */       path = "Sent Items" + path.substring(4);
/*     */     }
/*     */     
/*     */ 
/* 748 */     if ((this.mOwner != null) && (!this.mOwner.equals(""))) {
/* 749 */       path = "/home/" + this.mOwner + (path.equals("") ? "" : "/") + path;
/*     */     }
/* 751 */     return path;
/*     */   }
/*     */   
/*     */ 
/*     */   String asUtf7String()
/*     */   {
/* 757 */     return asUtf7String(asImapPath());
/*     */   }
/*     */   
/*     */   static String asUtf7String(String imapPath)
/*     */   {
/* 762 */     ByteBuffer bb = FOLDER_ENCODING_CHARSET.encode(imapPath);
/* 763 */     byte[] content = new byte[bb.limit() + 2];
/* 764 */     content[0] = 34;
/* 765 */     System.arraycopy(bb.array(), 0, content, 1, content.length - 2);
/* 766 */     content[(content.length - 1)] = 34;
/* 767 */     return new String(content).replaceAll("\\\\", "\\\\\\\\");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/ImapPath.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */